package com.lxb.myforum.security;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import com.lxb.myforum.security.exception.handler.AccessDeniedHandlerImpl;
import com.lxb.myforum.security.filter.LoginVerifyCodeFilter;

/**
 * Web security安全配置
 * @author lxb
 *
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private UserDetailsService userDetailsService;
	
	@Autowired
	private CustomFilterSecurityInterceptor customFilterSecurityInterceptor;
	
	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public void configure(WebSecurity web) throws Exception {
		super.configure(web);
		web.ignoring().antMatchers("/layui/**", "/js/**", "/styles/**", "/resources/**", "/images/**");
	}

	@Override
	protected void configure(HttpSecurity httpSecurity) throws Exception {
		httpSecurity
				.csrf().disable()
				.addFilterAt(customFilterSecurityInterceptor, FilterSecurityInterceptor.class)
				.authorizeRequests()
					// 允许用户登录注册
//					.antMatchers("/user/**").hasAnyRole("user", "admin")
//					.antMatchers("/admin/**").hasAnyRole("admin", "ADMIN")
					.antMatchers("/login/**", "/register/**", "/verifyCode").permitAll()
					.anyRequest().permitAll()
				.and()
				.formLogin()
					.loginPage("/login")
					.failureForwardUrl("/loginFail")
					.defaultSuccessUrl("/index")
					.permitAll()
				.and().logout()
					.logoutSuccessUrl("/index")
					.invalidateHttpSession(true)
					.permitAll()
					.and()
					.exceptionHandling().accessDeniedPage("/index")
					.accessDeniedHandler(new AccessDeniedHandlerImpl())
					
				.and().rememberMe().rememberMeParameter("rememberMe").tokenValiditySeconds(1209600);
		LoginVerifyCodeFilter loginVerifyCodeFilter = new LoginVerifyCodeFilter();
		httpSecurity.addFilterBefore(loginVerifyCodeFilter, UsernamePasswordAuthenticationFilter.class);
	}
	
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		super.configure(auth);
		auth.inMemoryAuthentication().withUser("admin1").password("111111").roles("ADMIN");
		auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
	}
}
